package com.tongzhou.tongzhou.order.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.tongzhou.db.DBUtil;
import com.tongzhou.db.exception.DBException;
import com.tongzhou.tongzhou.order.dao.IOrderDAO;
import com.tongzhou.tongzhou.order.model.GoodsOrder;
import com.tongzhou.tongzhou.order.model.SearchOrderCondition;
import com.tongzhou.util.Util;

public class OrderDAO implements IOrderDAO{

	@Override
	public List<GoodsOrder> getOrderList(GoodsOrder order) throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("select p.id,p.account_id,p.paper_id,p.degree_id,p.paper_name,");
		sql.append("p.lb,p.paperHeigth,p.paperWidth,p.count,p.status,p.order_date,p.finish_date ");
		sql.append(" from goods_order p inner join account a on a.id=p.account_id ");
		sql.append(" where 1=1 ");
		if(order.getCompany_name()!=null){
			sql.append(" and a.company_name='"+order.getCompany_name()+"' ");
		}
		if(order.getCompany_num()!=null){
			sql.append(" and a.company_num='"+order.getCompany_num()+"' ");
		}
		if(order.getId()!=0){
			sql.append(" and p.id='"+order.getId()+"' ");
		}
		if(order.getPaper_id()!=0){
			sql.append(" and p.paper_id='"+order.getPaper_id()+"' ");
		}
		if(order.getPaper_id()!=0){
			sql.append(" and p.degree_id='"+order.getDegree_id()+"' ");
		}
		if(order.getPaper_id()!=0){
			sql.append(" and date_format(p.finish_date,'%Y-%m-%d')='"+order.getFinish_date()+"' ");
		}
		List<GoodsOrder> result=DBUtil.query(sql.toString(),new BeanListHandler<GoodsOrder>(GoodsOrder.class));
		return result;
	}
	
	
	@Override
	public List<GoodsOrder> getOrderListByPage(SearchOrderCondition condition)
			throws DBException {
		List<GoodsOrder> result=null;
		
		//查询条件SQL
		//查询参数
		StringBuffer sqlCondition = new StringBuffer() ;
		List<String> paramList = new ArrayList<String>() ;
		
		if(condition.getCompany_name() != null && !condition.getCompany_name().equals("")){
			sqlCondition.append(" and a.company_name =?");
			paramList.add(condition.getCompany_name());
		}
		if(condition.getCompany_name() != null && !condition.getCompany_name().equals("")){
			sqlCondition.append(" and a.company_num =?") ;
			paramList.add(condition.getCompany_num()) ;
		}
		if(condition.getOid() != null && condition.getOid()!=0){
			sqlCondition.append(" and p.id =?") ;
			paramList.add(String.valueOf(condition.getOid())) ;
		}
		
		if((condition.getOid() == null || condition.getOid()==0)){
			if(condition.getPaper_id() != null && condition.getPaper_id()!=0){
				sqlCondition.append(" and p.paper_id =?") ;
				paramList.add(String.valueOf(condition.getPaper_id())) ;
			}
			if(condition.getDegree_id() != null && condition.getDegree_id()!=0){
				sqlCondition.append(" and p.degree_id =?") ;
				paramList.add(String.valueOf(condition.getDegree_id())) ;
			}
			if(condition.getFinish_date() != null && !condition.getFinish_date().equals("")){
				sqlCondition.append(" and  date_format(p.finish_date,'%Y-%m-%d') =?") ;
				paramList.add(condition.getFinish_date()) ;
			}
		}
		
		
		//取得满足条件的总记录数和总页数
		StringBuffer sql = new StringBuffer() ;
		sql.append("select count(p.id)");
		sql.append(" from goods_order p inner join account a on a.id=p.account_id ");
		sql.append(" where 1=1 ");
		sql.append(sqlCondition);
		
		
		//总记录数
		Map<String,String> dataMap=new HashMap<String,String>();
		long total = DBUtil.getCount(sql.toString(),paramList.toArray()) ;
		//总页数
		long totalPage = Util.getTotalPage(total, condition.getPageSize());
		
		condition.setTotal(total);
		condition.setTotalPage(totalPage);
		
		//查询某页的列表数据
		sql.delete(0, sql.length());
		sql.append("select p.id,p.account_id,p.paper_id,p.degree_id,p.paper_name,");
		sql.append("p.lb,p.paperHeigth,p.paperWidth,p.count,p.status,p.order_date,p.finish_date ");
		sql.append(" from goods_order p inner join account a on a.id=p.account_id ");
		sql.append(" where 1=1 ");
		sql.append(sqlCondition);
		sql.append(" order by "+condition.getOrderBy()+" "+condition.getOrderByType()) ;
		result = DBUtil.query(sql.toString(), new BeanListHandler<GoodsOrder>(GoodsOrder.class), paramList.toArray(), Util.getStartPos(condition.getCurrentPage(), condition.getPageSize()), condition.getPageSize()) ;
		return result;
	}

	@Override
	public GoodsOrder getOrdertById(GoodsOrder order) throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("select p.id,p.account_id,p.paper_id,p.degree_id,p.paper_name,");
		sql.append("p.lb,p.paperHeigth,p.paperWidth,p.count,p.status,");
		sql.append("date_format(p.order_date,'%Y-%m-%d') as order_date,");
		sql.append("date_format(p.finish_date,'%Y-%m-%d') as finish_date,p.price ");
		sql.append(" from goods_order p inner join account a on a.id=p.account_id ");
		sql.append(" where 1=1 ");
		sql.append(" and a.company_num='"+order.getCompany_num()+"' ");
		sql.append(" and p.id="+order.getId());
		GoodsOrder o=DBUtil.query(sql.toString(), new BeanHandler<GoodsOrder>(GoodsOrder.class));
		return o;
	}

}
